<?php
require("libraries/student.php");
class Destination extends Student{
	public function __construct(){
		parent::__construct();
		$this->load->model("model_destination");
		$this->load->library("string");
	}
	public function index(){
		$data['data'] = "";
		$data['act'] = 8;
		$data['title'] = "Manage destinations";
		$data['template'] = "destinations/list";
		$data['listregion'] = $this->model_destination->listregion();
		if(isset($_POST['ok'])){
			if($_POST['destination'] == 0){
				die("Please select region");
			}else{
				$destina = $_POST['destination'];
				$regions = $_POST['regions'];
				$and = array();
				foreach($regions as $items){
					$check = $this->model_destination->checkdestina($destina,$items);
					$desname = $this->model_destination->getdata($destina);
					$rename = $this->model_destination->getdata($items);
					$and[] = $items;
					if($check == 0){
						$url = str_replace(' ','-',strtolower($this->string->replace($desname['region_title']."-to-".$rename['region_title'])));
						$db = array(
						  "desti_name" => $desname['region_title']." - ".$rename['region_title'],
						  "fromname" => $desname['region_title'],
						  "toname" => $rename['region_title'],
						  "fromid"=>$destina,
						  "url" => $url,
						  "toid"=>$items,
						  "fromtoid"=>$destina.$items
						);
						$this->model_destination->add($db);
						
					}
				}
				if($and != NULL){
					$this->model_destination->deldestina($destina,$and);
					//die();
				}
				redirect(base_url()."admin");
			}
		}else{
		//$this->debug($data['listregion']);
		$this->load->view("layout",$data);
		}
	}
	public function setdestion(){
		$fromid = $this->uri->segment(4);
		$toid = $this->uri->segment(5);
		$data['result'] = $this->model_destination->setdestion($fromid,$toid);
		//$this->debug($data['result']);
		if($data['result'] == NULL){redirect(base_url()."admin/destination");}
		$data['title'] = "Set destinations";
		$data['data'] = "";
		$data['template'] = "destinations/value";
		if(isset($_POST['ok'])){
			$value = array("destination" => $_POST['destination']);
			$this->model_destination->update($value,$fromid,$toid);
			redirect(base_url()."admin/destination");
		}else{
			$this->load->view("layout",$data);
		}
	}
	public function listregion(){
		$id = $_POST['id'];
		$listregion = $this->model_destination->getregion($id);
		$destina = $this->model_destination->getdestina($id);
		if($destina != NULL){
			foreach($destina as $abc){
				$true[] = $abc['toid'];
			}
		}else{
			$true = array();
		}
		$i=1;
		echo "<p style='color:#F00'>Press in region to set destination</p>";
		echo "<div style='width:200px;float:left'>";
		foreach($listregion as $items){
			echo "<input type='checkbox' name='regions[]' class='region' value='".$items['region_id']."' ";
			if(in_array($items['region_id'],$true)){ echo "checked='checked'";}
			echo " /><a href='".base_url()."admin/destination/setdestion/$id/".$items['region_id']."' target='_blank'>".$items['region_title']."</a><br />";
			if($i == 6){
				break;
			}
			$i++;
		}
		echo "</div>";
		echo "<div style='width:200px;float:left'>";
		$j=1;
		foreach($listregion as $items){
			if($j > 6){
				echo "<input type='checkbox' name='regions[]' class='region' value='".$items['region_id']."' ";
				//if($items['region_id'] == $destina['toid']){ echo "checked='checked'";}
				if(in_array($items['region_id'],$true)){ echo "checked='checked'";}
				echo " /><a href='".base_url()."admin/destination/setdestion/$id/".$items['region_id']."' target='_blank'>".$items['region_title']."</a><br />";
			}
			$j++;
		}
		echo "</div>";
	}
}